﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace SimpleRcon.Windows
{
    /// <summary>
    /// Interaction logic for ChangeMapWindow.xaml
    /// </summary>
    public partial class ChangeMapWindow : Window
    {
        private SourceRconLib.Rcon Rcon;
        private BackgroundWorker bgWorker = new BackgroundWorker();

        public ChangeMapWindow(SourceRconLib.Rcon rcon)
        {
            InitializeComponent();

            if (rcon == null || !rcon.Connected)
            {
                MessageBox.Show("The Rcon Connection needs to be connected");
                return;
            }

            // Set
            Rcon = rcon;

            // Worker events
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);

            // Run Worker
            bgWorker.RunWorkerAsync();
        }

        #region Worker

        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            // Get maps
            string raw = Rcon.ServerCommandBlocking("maps *");
            raw = Regex.Split(raw, "\nL")[0];

            List<string> result = new List<string>();
            foreach (string item in raw.Split('\n'))
            {
                // skip the line that says ---------
                if (item.StartsWith("----"))
                {
                    continue;
                }

                string tmpStr = (item.Split(' ').Last());
                tmpStr = tmpStr.Remove(tmpStr.Length - 4);
                result.Add(tmpStr);
            }

            e.Result = result.ToArray();
        }

        void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(string.Format("Error: {0}", e.Error.Message));
            }
            else
            {
                // Set and enable
                listbox_maplist.Items.Clear();
                listbox_maplist.ItemsSource = (string[])e.Result;
                listbox_maplist.IsEnabled = true;
            }
        }

        #endregion

        #region Form Events

        private void button_Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button_ChangeMap_Click(object sender, RoutedEventArgs e)
        {
            if (listbox_maplist.SelectedItem is string)
            {
                string mapName = listbox_maplist.SelectedItem.ToString();
                Rcon.ServerCommand(string.Format("say Changing map to: {0}", mapName));
                Rcon.ServerCommand(string.Format("changelevel {0}", mapName));
                Close();
            }
        }

        #endregion

    }
}
